<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Employee - Parsing into structs</title>
<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Spirit X3 3.10">
<link rel="up" href="../tutorials.html" title="Tutorials">
<link rel="prev" href="roman.html" title="Roman Numerals">
<link rel="next" href="minimal.html" title="X3 Program Structure">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="roman.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="minimal.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="spirit_x3.tutorials.employee"></a><a class="link" href="employee.html" title="Employee - Parsing into structs">Employee - Parsing into
      structs</a>
</h3></div></div></div>
<p>
        It's a common question in the <a href="https://lists.sourceforge.net/lists/listinfo/spirit-general" target="_top">Spirit
        Mailing List</a>: How do I parse and place the results into a C++ struct?
        Of course, at this point, you already know various ways to do it, using semantic
        actions. There are many ways to skin a cat. Spirit X3, being fully attributed,
        makes it even easier. The next example demonstrates some features of Spirit
        X3 that make this easy. In the process, you'll learn about:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            More about attributes
          </li>
<li class="listitem">
            Auto rules
          </li>
<li class="listitem">
            Some more built-in parsers
          </li>
<li class="listitem">
            Directives
          </li>
</ul></div>
<p>
        First, let's create a struct representing an employee:
      </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">client</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">ast</span>
<span class="special">{</span>
    <span class="keyword">struct</span> <span class="identifier">employee</span>
    <span class="special">{</span>
        <span class="keyword">int</span> <span class="identifier">age</span><span class="special">;</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">forename</span><span class="special">;</span>
        <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">surname</span><span class="special">;</span>
        <span class="keyword">double</span> <span class="identifier">salary</span><span class="special">;</span>
    <span class="special">};</span>
<span class="special">}}</span>
</pre>
<p>
        Then, we need to tell <a href="../../../../../../../libs/fusion/doc/html/index.html" target="_top">Boost.Fusion</a>
        about our employee struct to make it a first-class fusion citizen that the
        grammar can utilize. If you don't know fusion yet, it is a <a href="http://www.boost.org/" target="_top">Boost</a>
        library for working with heterogeneous collections of data, commonly referred
        to as tuples. Spirit uses fusion extensively as part of its infrastructure.
      </p>
<p>
        In fusion's view, a struct is just a form of a tuple. You can adapt any struct
        to be a fully conforming fusion tuple:
      </p>
<pre class="programlisting"><span class="identifier">BOOST_FUSION_ADAPT_STRUCT</span><span class="special">(</span>
    <span class="identifier">client</span><span class="special">::</span><span class="identifier">ast</span><span class="special">::</span><span class="identifier">employee</span><span class="special">,</span>
    <span class="identifier">age</span><span class="special">,</span> <span class="identifier">forename</span><span class="special">,</span> <span class="identifier">surname</span><span class="special">,</span> <span class="identifier">salary</span>
<span class="special">)</span>
</pre>
<p>
        Now we'll write a parser for our employee. Inputs will be of the form:
      </p>
<pre class="programlisting"><span class="identifier">employee</span><span class="special">{</span> <span class="identifier">age</span><span class="special">,</span> <span class="string">"forename"</span><span class="special">,</span> <span class="string">"surname"</span><span class="special">,</span> <span class="identifier">salary</span> <span class="special">}</span>
</pre>
<p>
        <a name="__tutorial_employee_parser__"></a>Here goes:
      </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">parser</span>
<span class="special">{</span>
    <span class="keyword">namespace</span> <span class="identifier">x3</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">x3</span><span class="special">;</span>
    <span class="keyword">namespace</span> <span class="identifier">ascii</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">x3</span><span class="special">::</span><span class="identifier">ascii</span><span class="special">;</span>

    <span class="keyword">using</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">int_</span><span class="special">;</span>
    <span class="keyword">using</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">lit</span><span class="special">;</span>
    <span class="keyword">using</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">double_</span><span class="special">;</span>
    <span class="keyword">using</span> <span class="identifier">x3</span><span class="special">::</span><span class="identifier">lexeme</span><span class="special">;</span>
    <span class="keyword">using</span> <span class="identifier">ascii</span><span class="special">::</span><span class="identifier">char_</span><span class="special">;</span>

    <span class="identifier">x3</span><span class="special">::</span><span class="identifier">rule</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">employee</span><span class="special">,</span> <span class="identifier">ast</span><span class="special">::</span><span class="identifier">employee</span><span class="special">&gt;</span> <span class="keyword">const</span> <span class="identifier">employee</span> <span class="special">=</span> <span class="string">"employee"</span><span class="special">;</span>

    <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">quoted_string</span> <span class="special">=</span> <span class="identifier">lexeme</span><span class="special">[</span><span class="char">'"'</span> <span class="special">&gt;&gt;</span> <span class="special">+(</span><span class="identifier">char_</span> <span class="special">-</span> <span class="char">'"'</span><span class="special">)</span> <span class="special">&gt;&gt;</span> <span class="char">'"'</span><span class="special">];</span>

    <span class="keyword">auto</span> <span class="keyword">const</span> <span class="identifier">employee_def</span> <span class="special">=</span>
        <span class="identifier">lit</span><span class="special">(</span><span class="string">"employee"</span><span class="special">)</span>
        <span class="special">&gt;&gt;</span> <span class="char">'{'</span>
        <span class="special">&gt;&gt;</span>  <span class="identifier">int_</span> <span class="special">&gt;&gt;</span> <span class="char">','</span>
        <span class="special">&gt;&gt;</span>  <span class="identifier">quoted_string</span> <span class="special">&gt;&gt;</span> <span class="char">','</span>
        <span class="special">&gt;&gt;</span>  <span class="identifier">quoted_string</span> <span class="special">&gt;&gt;</span> <span class="char">','</span>
        <span class="special">&gt;&gt;</span>  <span class="identifier">double_</span>
        <span class="special">&gt;&gt;</span>  <span class="char">'}'</span>
        <span class="special">;</span>

    <span class="identifier">BOOST_SPIRIT_DEFINE</span><span class="special">(</span><span class="identifier">employee</span><span class="special">);</span>
<span class="special">}</span>
</pre>
<p>
        The full cpp file for this example can be found here: <a href="../../../../../example/x3/employee.cpp" target="_top">employee.cpp</a>
      </p>
<p>
        Let's walk through this one step at a time (not necessarily from top to bottom).
      </p>
<h5>
<a name="spirit_x3.tutorials.employee.h0"></a>
        <span class="phrase"><a name="spirit_x3.tutorials.employee.rule_declaration"></a></span><a class="link" href="employee.html#spirit_x3.tutorials.employee.rule_declaration">Rule
        Declaration</a>
      </h5>
<p>
        We are assuming that you already know about rules. We introduced rules in
        the previous <a class="link" href="roman.html" title="Roman Numerals">Roman Numerals example</a>.
        Please go back and review the previous tutorial if you have to.
      </p>
<pre class="programlisting"><span class="identifier">x3</span><span class="special">::</span><span class="identifier">rule</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">employee</span><span class="special">,</span> <span class="identifier">ast</span><span class="special">::</span><span class="identifier">employee</span><span class="special">&gt;</span> <span class="identifier">employee</span> <span class="special">=</span> <span class="string">"employee"</span><span class="special">;</span>
</pre>
<h5>
<a name="spirit_x3.tutorials.employee.h1"></a>
        <span class="phrase"><a name="spirit_x3.tutorials.employee.lexeme"></a></span><a class="link" href="employee.html#spirit_x3.tutorials.employee.lexeme">Lexeme</a>
      </h5>
<pre class="programlisting"><span class="identifier">lexeme</span><span class="special">[</span><span class="char">'"'</span> <span class="special">&gt;&gt;</span> <span class="special">+(</span><span class="identifier">char_</span> <span class="special">-</span> <span class="char">'"'</span><span class="special">)</span> <span class="special">&gt;&gt;</span> <span class="char">'"'</span><span class="special">];</span>
</pre>
<p>
        <code class="computeroutput"><span class="identifier">lexeme</span></code> inhibits space skipping
        from the open brace to the closing brace. The expression parses quoted strings.
      </p>
<pre class="programlisting"><span class="special">+(</span><span class="identifier">char_</span> <span class="special">-</span> <span class="char">'"'</span><span class="special">)</span>
</pre>
<p>
        parses one or more chars, except the double quote. It stops when it sees
        a double quote.
      </p>
<h5>
<a name="spirit_x3.tutorials.employee.h2"></a>
        <span class="phrase"><a name="spirit_x3.tutorials.employee.difference"></a></span><a class="link" href="employee.html#spirit_x3.tutorials.employee.difference">Difference</a>
      </h5>
<p>
        The expression:
      </p>
<pre class="programlisting"><span class="identifier">a</span> <span class="special">-</span> <span class="identifier">b</span>
</pre>
<p>
        parses <code class="computeroutput"><span class="identifier">a</span></code> but not <code class="computeroutput"><span class="identifier">b</span></code>. Its attribute is just <code class="computeroutput"><span class="identifier">A</span></code>; the attribute of <code class="computeroutput"><span class="identifier">a</span></code>.
        <code class="computeroutput"><span class="identifier">b</span></code>'s attribute is ignored.
        Hence, the attribute of:
      </p>
<pre class="programlisting"><span class="identifier">char_</span> <span class="special">-</span> <span class="char">'"'</span>
</pre>
<p>
        is just <code class="computeroutput"><span class="keyword">char</span></code>.
      </p>
<h5>
<a name="spirit_x3.tutorials.employee.h3"></a>
        <span class="phrase"><a name="spirit_x3.tutorials.employee.plus"></a></span><a class="link" href="employee.html#spirit_x3.tutorials.employee.plus">Plus</a>
      </h5>
<pre class="programlisting"><span class="special">+</span><span class="identifier">a</span>
</pre>
<p>
        is similar to Kleene star. Rather than match everything, <code class="computeroutput"><span class="special">+</span><span class="identifier">a</span></code> matches one or more. Like it's related
        function, the Kleene star, its attribute is a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span></code>
        where <code class="computeroutput"><span class="identifier">A</span></code> is the attribute
        of <code class="computeroutput"><span class="identifier">a</span></code>. So, putting all these
        together, the attribute of
      </p>
<pre class="programlisting"><span class="special">+(</span><span class="identifier">char_</span> <span class="special">-</span> <span class="char">'"'</span><span class="special">)</span>
</pre>
<p>
        is then:
      </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span>
</pre>
<h5>
<a name="spirit_x3.tutorials.employee.h4"></a>
        <span class="phrase"><a name="spirit_x3.tutorials.employee.sequence_attribute"></a></span><a class="link" href="employee.html#spirit_x3.tutorials.employee.sequence_attribute">Sequence
        Attribute</a>
      </h5>
<p>
        Now what's the attribute of
      </p>
<pre class="programlisting"><span class="char">'"'</span> <span class="special">&gt;&gt;</span> <span class="special">+(</span><span class="identifier">char_</span> <span class="special">-</span> <span class="char">'"'</span><span class="special">)</span> <span class="special">&gt;&gt;</span> <span class="char">'"'</span>
</pre>
<p>
        ?
      </p>
<p>
        Well, typically, the attribute of:
      </p>
<pre class="programlisting"><span class="identifier">a</span> <span class="special">&gt;&gt;</span> <span class="identifier">b</span> <span class="special">&gt;&gt;</span> <span class="identifier">c</span>
</pre>
<p>
        is:
      </p>
<pre class="programlisting"><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">B</span><span class="special">,</span> <span class="identifier">C</span><span class="special">&gt;</span>
</pre>
<p>
        where <code class="computeroutput"><span class="identifier">A</span></code> is the attribute
        of <code class="computeroutput"><span class="identifier">a</span></code>, <code class="computeroutput"><span class="identifier">B</span></code>
        is the attribute of <code class="computeroutput"><span class="identifier">b</span></code> and
        <code class="computeroutput"><span class="identifier">C</span></code> is the attribute of <code class="computeroutput"><span class="identifier">c</span></code>. What is <code class="computeroutput"><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector</span></code>?
        - a tuple.
      </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          If you don't know what I am talking about, see: <a href="http://tinyurl.com/6xun4j" target="_top">Fusion
          Vector</a>. It might be a good idea to have a look into <a href="../../../../../../../libs/fusion/doc/html/index.html" target="_top">Boost.Fusion</a>
          at this point. You'll definitely see more of it in the coming pages.
        </p></td></tr>
</table></div>
<h5>
<a name="spirit_x3.tutorials.employee.h5"></a>
        <span class="phrase"><a name="spirit_x3.tutorials.employee.attribute_collapsing"></a></span><a class="link" href="employee.html#spirit_x3.tutorials.employee.attribute_collapsing">Attribute
        Collapsing</a>
      </h5>
<p>
        Some parsers, especially those very little literal parsers you see, like
        <code class="computeroutput"><span class="char">'"'</span></code>, do not have attributes.
      </p>
<p>
        Nodes without attributes are disregarded. In a sequence, like above, all
        nodes with no attributes are filtered out of the <code class="computeroutput"><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector</span></code>.
        So, since <code class="computeroutput"><span class="char">'"'</span></code> has no attribute,
        and <code class="computeroutput"><span class="special">+(</span><span class="identifier">char_</span>
        <span class="special">-</span> <span class="char">'"'</span><span class="special">)</span></code> has a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span></code>
        attribute, the whole expression's attribute should have been:
      </p>
<pre class="programlisting"><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span> <span class="special">&gt;</span>
</pre>
<p>
        But wait, there's one more collapsing rule: If the attribute is followed
        by a single element <code class="computeroutput"><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector</span></code>,
        The element is stripped naked from its container. To make a long story short,
        the attribute of the expression:
      </p>
<pre class="programlisting"><span class="char">'"'</span> <span class="special">&gt;&gt;</span> <span class="special">+(</span><span class="identifier">char_</span> <span class="special">-</span> <span class="char">'"'</span><span class="special">)</span> <span class="special">&gt;&gt;</span> <span class="char">'"'</span>
</pre>
<p>
        is:
      </p>
<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">char</span><span class="special">&gt;</span>
</pre>
<h5>
<a name="spirit_x3.tutorials.employee.h6"></a>
        <span class="phrase"><a name="spirit_x3.tutorials.employee.rule_definition"></a></span><a class="link" href="employee.html#spirit_x3.tutorials.employee.rule_definition">Rule
        Definition</a>
      </h5>
<p>
        Again, we are assuming that you already know about rules and rule definitions.
        We introduced rules in the previous <a class="link" href="roman.html" title="Roman Numerals">Roman
        Numerals example</a>. Please go back and review the previous tutorial
        if you have to.
      </p>
<pre class="programlisting"><span class="identifier">employee</span> <span class="special">=</span>
    <span class="identifier">lit</span><span class="special">(</span><span class="string">"employee"</span><span class="special">)</span>
    <span class="special">&gt;&gt;</span> <span class="char">'{'</span>
    <span class="special">&gt;&gt;</span>  <span class="identifier">int_</span> <span class="special">&gt;&gt;</span> <span class="char">','</span>
    <span class="special">&gt;&gt;</span>  <span class="identifier">quoted_string</span> <span class="special">&gt;&gt;</span> <span class="char">','</span>
    <span class="special">&gt;&gt;</span>  <span class="identifier">quoted_string</span> <span class="special">&gt;&gt;</span> <span class="char">','</span>
    <span class="special">&gt;&gt;</span>  <span class="identifier">double_</span>
    <span class="special">&gt;&gt;</span>  <span class="char">'}'</span>
    <span class="special">;</span>

<span class="identifier">BOOST_SPIRIT_DEFINE</span><span class="special">(</span><span class="identifier">employee</span><span class="special">);</span>
</pre>
<p>
        Applying our collapsing rules above, the RHS has an attribute of:
      </p>
<pre class="programlisting"><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span>
</pre>
<p>
        These nodes do not have an attribute:
      </p>
<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
<li class="listitem">
            <code class="computeroutput"><span class="identifier">lit</span><span class="special">(</span><span class="string">"employee"</span><span class="special">)</span></code>
          </li>
<li class="listitem">
            <code class="computeroutput"><span class="char">'{'</span></code>
          </li>
<li class="listitem">
            <code class="computeroutput"><span class="char">','</span></code>
          </li>
<li class="listitem">
            <code class="computeroutput"><span class="char">'}'</span></code>
          </li>
</ul></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          In case you are wondering, <code class="computeroutput"><span class="identifier">lit</span><span class="special">(</span><span class="string">"employee"</span><span class="special">)</span></code> is the same as "employee". We
          had to wrap it inside <code class="computeroutput"><span class="identifier">lit</span></code>
          because immediately after it is <code class="computeroutput"><span class="special">&gt;&gt;</span>
          <span class="char">'{'</span></code>. You can't right-shift a <code class="computeroutput"><span class="keyword">char</span><span class="special">[]</span></code> and
          a <code class="computeroutput"><span class="keyword">char</span></code> - you know, C++ syntax
          rules.
        </p></td></tr>
</table></div>
<p>
        Recall that the attribute of <code class="computeroutput"><span class="identifier">parser</span><span class="special">::</span><span class="identifier">employee</span></code>
        is the <code class="computeroutput"><span class="identifier">ast</span><span class="special">::</span><span class="identifier">employee</span></code> struct.
      </p>
<p>
        Now everything is clear, right? The <code class="computeroutput"><span class="keyword">struct</span>
        <span class="identifier">employee</span></code> <span class="bold"><strong>IS</strong></span>
        compatible with <code class="computeroutput"><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">double</span><span class="special">&gt;</span></code>.
        So, the RHS of <code class="computeroutput"><span class="identifier">start</span></code> uses
        start's attribute (a <code class="computeroutput"><span class="keyword">struct</span> <span class="identifier">employee</span></code>) in-situ when it does its work.
      </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2018 Joel de Guzman,
      Hartmut Kaiser<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="roman.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.html"><img src="../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="minimal.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
